package com.nd.qa.shareplate.script.realmonkey;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.nd.qa.shareplate.model.Module;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.model.Project;
import com.nd.qa.shareplate.script.junit.PythonJunitScript;
import com.nd.qa.shareplate.util.FileUtil;

/**
 * 拼接python脚本工具类
 * 
 * @version 1.00 wuqiaomin 新建
 *
 */
public class RealMonkeyScriptTool {
	OutputStreamWriter python = null;
	StringBuilder content = null;
	Module program = null;
	String modelDirPath = null;
	RealMonkey realMonkey = new RealMonkey();
	PythonJunitScript junitScript = new PythonJunitScript();
	FileUtil fileUtil = new FileUtil();
	Project project = null;

	/**
	 * 初始化创建脚本过程数据
	 * @param program	模块信息
	 * @param project	项目信息
	 * @param projectDirPath	创建的测试工程路径
	 */
	public RealMonkeyScriptTool(Module program, Project project,
			String projectDirPath) {
		this.program = program;
		// this.operational = iDataService.getOperational();
		this.modelDirPath = projectDirPath;
		this.project = project;
	}

	private void createScript() {
		content = new StringBuilder();
		String fileName = this.modelDirPath + this.program.getModuleName();

		File file = fileUtil.createFile(fileName, ".py");
		try {
			python = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
			python.write(realMonkey.scriptInit());
			python.write(createClass("ModelName"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private String setUp() {
		StringBuilder testCase = new StringBuilder();
		testCase.append("print 'xml_file_path#'+ROOTPATH+'/xml'\n");
		testCase.append("print 'TestModelName#', package_name\n");
		return junitScript.setUp(testCase.toString());
	}

	private String createClass(String className) {
		return String.format("\nclass %s(unittest.TestCase):\n", className);
	}

	private String setComponent() {
		return realMonkey.startApp(project.getProjectPackage(),
				project.getProjectMainActivity());
	}

	private String writeCode(PageElement element) {
		StringBuilder sb = new StringBuilder();
		String operate = element.getElementActionName();
		if (operate.contains("drag")) {
			sb.append(realMonkey.drag(element));
			sb.append(realMonkey.sleep("4.0"));
		} else {
			switch (operate) {
			case "click":
				sb.append(realMonkey.click(element));
				sb.append(realMonkey.sleep("4.0"));
				break;
			case "input":
				sb.append(realMonkey.enterText(element));
				sb.append(realMonkey.sleep("4.0"));
			default:
				break;
			}
		}
		return sb.toString();
	}

	private void endWrite() {
		if (python != null) {
			try {
				content.append(junitScript.mainMethod());
				python.write(content.toString());
				python.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void generateScript(List<List<PageElement>> scriptList) {
		createScript();
		try {
			content.append(setUp());
			for (List<PageElement> list : scriptList) {
				StringBuilder sb = new StringBuilder();
				PageElement element = list.get(0);
				String caseName = String.format("%s%s", element.getElementTypeName(),
						element.getElementId());
				sb.append(String
						.format("logPath = modelname+'/%s'\n", caseName));
				sb.append("device=rMonkeyRunner(logPath)\n");
				sb.append(setComponent());
				System.out.println("-----------------------------------------");
				int size = list.size();
				for (int i = size - 1; i >= 0; i--) {
					sb.append(writeCode(list.get(i)));
					System.out.println(list.get(i).toString());
				}
				content.append(junitScript.testCase(caseName, sb.toString()));
				System.out.println("-----------------------------------------");
			}
			endWrite();
		} catch (Exception e) {
			endWrite();
			e.printStackTrace();
		}
	}
}
